package dwz.cms.article.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.ibatis.session.RowBounds;
import org.springframework.stereotype.Service;

import dwz.persistence.ArticleConditionVO;
import dwz.persistence.beans.Article;
import dwz.persistence.mapper.ArticleMapper;
import dwz.persistence.mapper.CmsArtChannelMapper;
/** 
* Created by Eclipse
* @author : Yating
* @version:2017年3月27日 下午6:53:16 
* 文章显示服务类 
*/
@Service("articleDispService")
public class ArticleDispService implements IArticleDisp{
	
	@Resource 
	ArticleMapper articleMapper;
	
	@Resource 
	CmsArtChannelMapper cmsArticleChannelMapper;

	/**
	 * 获取所有频道列表
	 */
	@Override
	public List<Article> getAllArticleList(){
		List<Article> articleList=articleMapper.selectArticleList();
		
		return articleList;
	}
	
	/**
	 * 根据频道id号获取该频道下文章列表
	 */
	@Override
	public List<Article> getArticleByChannelId(int channelid){
		List<String> codeList=cmsArticleChannelMapper.selectArticleListByChannelid(channelid);
		List<Article> articleList=articleMapper.selectArticleListByCodeList(codeList);
		//List<Article> articleList=articleMapper.selectArticleList();
		//List<Article> articleList=null;
		return articleList;
	}
	
	/**
	 * 根据 文章code获取该篇文章
	 */
	@Override
	public Article getArticle(String code){
		return articleMapper.selectArticleByCode(code);
		//return cmsArticlePublishMapper.selectArticleByCode(code);
	}
	
	/**
	 * 根据文章code列表 获取对应 文章列表
	 */
	@Override
	public List<Article> getArticleByCodeList(List<String> codeList){
		return articleMapper.selectArticleListByCodeList(codeList);
		//return cmsArticlePublishMapper.selectArticleByCode(code);
	}
	
	/**
	 * 查找文章
	 */
	@Override
	public Map<String, List> searchArticle(ArticleConditionVO vo,int channelid) {
		List<Article> bos = new ArrayList<Article>();
		List<String> articleReturnList=new ArrayList<String>(); 
		List<Short> topList=new ArrayList<Short>();
		
		RowBounds rb = new RowBounds(vo.getStartIndex(), vo.getPageSize());
		
		List<String> codeList=cmsArticleChannelMapper.selectArticleListByChannelid(channelid);
		vo.setCodeList(codeList);
		
		List<Article> pos = articleMapper.findPageBreakByCondition(vo, rb);
		for (Article po : pos) {
			bos.add(po);
			articleReturnList.add(po.getCode());
		}
		
		topList=cmsArticleChannelMapper.getChannelTopByCodeListandChannelId(articleReturnList, channelid);
		
		
		Map<String, List> map = new HashMap<String, List>();
		map.put("articleList", bos);
		map.put("topList", topList);

		
		return map;
	}

	// TODO
	@Override
	public Integer searchArticleNum(ArticleConditionVO vo,int channelid) {
		List<String> codeList=cmsArticleChannelMapper.selectArticleListByChannelid(channelid);
		vo.setCodeList(codeList);
		Integer count = articleMapper.findNumberByCondition(vo);

		return count;
	}
	
	
	
	
}
 